package spring.framework.beans.factory.support;


import spring.framework.beans.BeansException;
import spring.framework.beans.factory.BeanFactory;
import spring.framework.beans.factory.config.BeanDefinition;

/**
 * @author fun.pengzh
 * @class spring.framework.beans.factory.support.AbstractBeanFactory
 * @desc
 * @since 2021-09-18
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String name) throws BeansException {
        return doGetBean(name, null);
    }

    @Override
    public Object getBean(String name, Object... args) throws BeansException {
        return doGetBean(name, args);
    }

    Object doGetBean(final String name, final Object[] args) throws BeansException {
        Object bean = getSingleton(name);
        if (null == bean) {
            final BeanDefinition beanDefinition = getBeanDefinition(name);
            bean = createBean(name, beanDefinition, args);
        }
        addSingleton(name, bean);
        return bean;
    }


    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException;

    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

}
